package com.hxh.crud.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxh.crud.bean.Employee;
import com.hxh.crud.bean.EmployeeExample;
import com.hxh.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @CreatedBy: hxh
 * @Date: 2020/2/28 13:55
 * @Description: 员工逻辑层
 * @TODO: 加油吧！骚年！
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public PageInfo<Employee> getEmps(Integer pageNum, Integer pageSize, Map<String, Object> example) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpStatusEqualTo(1);
        // 将map对象转为javaBean对象
        ObjectMapper objectMapper = new ObjectMapper();
        Employee employee = objectMapper.convertValue(example.get("example"), Employee.class);
        if (employee != null) {
            if (employee.getEmpName().length() != 0) {
                criteria.andEmpNameLike("%" + employee.getEmpName() + "%");
            }
            if (employee.getEmail().length() != 0) {
                criteria.andEmailLike("%" + employee.getEmail() + "%");
            }
            if (employee.getSex().length() != 0) {
                criteria.andSexEqualTo(employee.getSex());
            }
            if (employee.getDId().length() != 0) {
                criteria.andDIdEqualTo(employee.getDId());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employeeList = employeeMapper.selectByExampleWithDept(employeeExample);
        return new PageInfo<>(employeeList);
    }

    public boolean checkEmp(String empName, String empId) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpStatusEqualTo(1);
        criteria.andEmpNameEqualTo(empName);
        if (empId != null) {
            criteria.andEmpIdNotEqualTo(empId);
        }
        long l = employeeMapper.countByExample(employeeExample);
        return l != 0;
    }

    public void addEmp(Employee employee) {
        if (employee.getEmpId() == null) {
            employee.setEmpId(UUID.randomUUID().toString());
        }
        employeeMapper.insertSelective(employee);
    }

    public Employee getEmpById(String empId) {
        return employeeMapper.selectByPrimaryKeyWithDept(empId);
    }

    public void editEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void delEmp(List<Employee> employeeList) {
        for (Employee employee : employeeList) {
            employee.setEmpStatus(0);
            employeeMapper.updateByPrimaryKeySelective(employee);
        }
    }

}
